/**
 * 订单管理管理初始化
 */
var TOrder = {
    id: "TOrderTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TOrder.initColumn = function () {
    return [
        {type: 'checkbox', fixed: 'left'},
        {title: '运单单号', field: 'original_num', width: 180, visible: true, align: 'center', valign: 'middle'},
        {title: '收货人', field: 'consignee', visible: true, align: 'center', valign: 'middle', fixed: 'left'},
        {title: '金额', field: 'sum', visible: true, align: 'center', valign: 'middle', fixed: 'left',
            templet: function (d) {
                return parseFloat(d.ys)-parseFloat(d.yf);
            }
        },
        {title: '入库时间', field: 'arrive_date', visible: true, align: 'center', valign: 'middle', fixed: 'left'},
        {title: '提货时间', field: 'check_time', visible: true, align: 'center', valign: 'middle', fixed: 'left'},
        {title: '回单', field: 'receipt_status', visible: true, align: 'center', valign: 'middle', fixed: 'left',
            templet: function (d) {
                if(d.receipt_status==0){
                    return "待回单"
                }else if(d.receipt_status==1 || d.receipt_status==2){
                    return "已回单"
                }else if(d.receipt_status==3){
                    return "无需回单"
                }
            }
        },
        {title: '状态', field: 'audit_status', visible: true, align: 'center', valign: 'middle', fixed: 'left',

            templet: function (d) {
                if(d.audit_status==3){
                    return "已核销"
                }
                return "待核销"

            }
        },
        {title: '制单员', field: 'create_user_id', visible: true, align: 'center', valign: 'middle', fixed: 'left'},
        {title: '核销人', field: 'create_user_id', visible: true, align: 'center', valign: 'middle', fixed: 'left'}


    ];
};

/**
 * 检查是否选中
 */
TOrder.check = function () {
    var selected = mTable.checkStatus(TOrder.id);
    if (selected.data.length == 0 || selected.data.length > 1) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        TOrder.seItem = selected.data[0];
        return true;
    }
};

/**
 * 检查是否选中
 */
TOrder.checks = function () {
    var selected = mTable.checkStatus(TOrder.id);
    if (selected.data.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }
    return true;
};
/**
 * 点击添加订单管理
 */
TOrder.openAddTOrder = function () {

    var index = layer.open({
        type: 2,
        title: '添加订单管理',
        shade:0.01,
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/tOrder/tOrder_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看订单管理详情
 */
TOrder.openTOrderDetail = function () {

    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '订单出库',
            area: ['1150px', '450px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/tOrder/hxLibrary/' + TOrder.seItem.id
        });
        layer.full(index);
        this.layerIndex = index;
    }
};

/**
 * 删除订单管理
 */
TOrder.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/tOrder/delete", function (data) {
            Feng.success("删除成功!");
            aTable.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("tOrderId", this.seItem.id);
        ajax.start();
    }
};
/**
 * 自提批量核销
 */
TOrder.batchCheck = function () {
    var selected = mTable.checkStatus(TOrder.id);
    if (selected.data.length <= 0) {
        Feng.info("请先选中表格中的订单！");
        return false;
    }
    var ids = new Array();
    for (var i = 0; i < selected.data.length; i++) {
        ids.push(selected.data[i].id);
    }
    Feng.confirm("确认批量核销？",function(){
        var ajax = new $ax(Feng.ctxPath + "/tOrder/selfLiftBatchCheck", function (data) {
            Feng.success("核销成功!");
            aTable.refresh();
        }, function (data) {
            Feng.error("核销失败!" + data.responseJSON.message + "!");
        });
        ajax.set("ids", ids.join(","));
        ajax.start();
    },function(){

    });

}
/**
 * 删除订单管理
 */
TOrder.checkOrder = function () {
    var selected = mTable.checkStatus(TOrder.id);
    var ids = new Array();
    for (var i = 0; i < selected.data.length; i++) {
        ids.push(selected.data[i].id);
    }
    if (this.checks()) {
        var ajax = new $ax(Feng.ctxPath + "/tOrder/checkOrder", function (data) {
            Feng.success("审核成功!");
            aTable.refresh();
        }, function (data) {
            Feng.error("审核失败!" + data.responseJSON.message + "!");
        });
        ajax.set("ids", ids.join(","));
        ajax.start();
    }
};
/**
 * 查询订单管理列表
 */
TOrder.search = function () {
    var queryData = {};
    queryData['name'] = $("#name").val();
    queryData['receipt_status'] = $("#receipt_status").val()+"";
    queryData['audit_status'] = $("#audit_status").val()+"";
    aTable.refresh(queryData);
};
var aTable = null
var mTable = null;
$(function () {
    var defaultColunms = TOrder.initColumn();
    var orderFlag = $('#orderFlag').val();
    var sUrl = "/tOrder/list"
    aTable = new LYTable(TOrder.id, sUrl, defaultColunms);
    layui.use('table', function () {
        mTable = layui.table;
        TOrder.table = aTable.init(mTable);
    })


    $('#receipt_status').selectpicker({
        style: 'btn btn-select',
        noneSelectedText:"回单状态"
    });
    $('#audit_status').selectpicker({
        style: 'btn btn-select',
        noneSelectedText:"出库状态"
    });
});
$(document).keyup(function(event) {
    if (event.keyCode == 13) {
        TOrder.search();
    }
});